﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

using Prism.Core.Audio.DirectSound;

namespace Prism.Core.Audio
{
    /// <summary>
    /// 表示一个音频。
    /// </summary>
    public class Audio : IAudio
    {
        protected DirectSoundAudio DirectSoundAudio { get; set; }

        protected object locker = new object();

        protected Audio()
        {

        }

        public Audio(System.IO.Stream stream, TimeSpan latency)
            : this()
        {
            this.DirectSoundAudio = new DirectSoundAudio((int)(latency.TotalMilliseconds));

            this.DirectSoundAudio.Load(AudioFileReader.GetInputStream(stream));
        }

        /// <summary>
        /// 当音频播放完毕时，触发该事件。
        /// </summary>
        /// <remarks>当循环播放时不会引发该事件，但是会在所有循环结束的时候引发。</remarks>
        public event EventHandler<AudioEndedEventArgs> MediaEnded;

        /// <summary>
        /// 播放当前的音频。
        /// </summary>
        public void Play(float volume)
        {
            this.Volume = volume;

            this.DirectSoundAudio.Play();
            this.DirectSoundAudio.PlaybackStopped += new EventHandler(delegate(object sender, EventArgs e)
            {
                this.OnMediaEnded(new AudioEndedEventArgs() { Audio = this });
            });

            this.IsPlaying = true;

            this.IsPaused = false;
            this.IsEnded = false;
        }

        /// <summary>
        /// 获取音频是否暂停。
        /// </summary>
        public bool IsPaused { get; protected set; }

        /// <summary>
        /// 获取音频是否已经播放完毕。
        /// </summary>
        public bool IsEnded { get; protected set; }

        /// <summary>
        /// 获取或者设置音频是否无限循环。
        /// </summary>
        public bool IsLoop { get { return this.DirectSoundAudio.IsLoop; } set { this.DirectSoundAudio.IsLoop = value; } }

        public float Volume { get { return this.DirectSoundAudio.Volume; } set { this.DirectSoundAudio.Volume = value; } }

        /// <summary>
        /// 暂停当前的音频。
        /// </summary>
        public void Pause()
        {
            this.DirectSoundAudio.Pause();
            this.IsPaused = true;
        }

        /// <summary>
        /// 停止播放当前的音频。
        /// </summary>
        public void Stop()
        {
            lock (this.locker)
            {
                this.DirectSoundAudio.Stop();
            }
        }

        /// <summary>
        /// 关闭音频。
        /// </summary>
        public void Close()
        {
            this.DirectSoundAudio.Dispose();
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public void Resume()
        {
            this.IsPaused = false;
        }

        public bool IsPlaying
        {
            get;
            private set;
        }

        public void ChangeAudioVolume(float volume)
        {
            this.Volume = volume;
        }

        protected virtual void OnMediaEnded(AudioEndedEventArgs e)
        {
            if (this.MediaEnded != null) this.MediaEnded(this, e);
        }
    }
}
